<?php
namespace Admin\Controller;
use Think\Controller;
/**
 * 关键字管理控制器
 */
class KeyWordsController extends  IsLoginController{

	/**
	 * 关键字自动回复管理
	 * 
	 */
	public function  index(){
		$uid= $_SESSION['uid'];
		$keyWord = M('wxkeywords')->where("wxkuid = '{$uid}'")->select();
		$this->assign('keyWord',$keyWord);
		$this->display();
	}
	
	 
	 
	 /**
	  * 添加关键字自动回复内容
	  */
	public function AddKeyWords(){
		if(IS_POST){
			$Words = D('wxkeywords');
			if(!$Words->addKeyWords()){ echo $Words->getError();
//	  			$this->error($Words->getError());
	  		}echo 'ok';
//			$this->success('自动回复内容保存成功');
	 	}
		$this->display();
	}
	
	
	/**
	* 用户关注时自动回复内容设置
	*/
	public function UserWelcom(){
	 	if(IS_POST){
			$userApi = D('wxapi');
			if(!$userApi->UserWelcom()){
	  			$this->error($userApi->getError());
	  		}
			$this->success('自动回复内容保存成功');
	 	}
		//查询用户的自动回复内容
		$uid = $_SESSION['uid'];
		$userInfo = M('wxapi')->where("wxuid='{$uid}'")->find();
		$this->assign('userInfo',$userInfo);
		$this->display();
	 }




	  /**
	  * 上传图片 异步uploadify 方法
	  */
	public function uploadImg(){
		if(IS_POST){		
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize   =     3145728 ;// 设置附件上传大小
		$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg','amr','mp4');// 设置附件上传类型
		$upload->savePath  =     'weixin/big/'; // 设置附件上传目录
		$upload->saveName  =    'duosute_'.date('ymdHis');
		$upload->autoSub   = 	true;
		$upload->callback  =    false;
		$upload->subName   =    false;
		$file =  $upload->upload();
//		if(!$info) {// 上传错误提示错误信息        
//				echo $upload->getError();
//		    }else{// 上传成功 获取上传文件信息        
//		    	 echo $info['savepath'].$info['savename'];
//			}
			//缩略图处理  一张图缩了三张图
			
//			echo "<script>console.log(".$file.")</script>";
			
			$path =  'Uploads/'.$file['Filedata']['savepath'].$file['Filedata']['savename'];//原图样式  大图样式
//			$this->ajaxReturn($path);
			
			$thumbPath = 'Uploads/'.$file['Filedata']['savepath'].'thumb/360X200_'.$file['Filedata']['savename']; 	//如果批量上传 	$file[0]['savepath'] 加0	
			$thumbPath= str_replace('big/', '', $thumbPath); //组合路径   缩图图路径样式  Uploads/weixin/thumb/100X100_duosute_150812155815.png
//			$this->ajaxReturn($thumbPath);
			$img =  new \Think\Image();
			$img->open($path);
			$img->thumb(360, 200,\Think\Image::IMAGE_THUMB_FIXED)->save($thumbPath);
			
			//循环组合图片路径 返回图片名
//			foreach($file as $k =>$v){
//				$data[$k]['fileName']= $v['savename'];
//			}
			$data['path'] = 'Uploads/'.$file['Filedata']['savepath'];
			$data['name'] = $file['Filedata']['savename'];
			$data['thumbPath'] = $thumbPath;
			unlink($path);//删除大图
			$this->ajaxReturn($data);
		}	
//			$this->display();
	}

	/**
	 *  noAnswer  未匹配到用户内容默认回复内容
	 */
	public function NoAnswer(){
	 	if(IS_POST){
			$userApi = D('wxapi');
			if(!$userApi->noAnswer()){
	  			$this->error($userApi->getError());
	  		}
			$this->success('内容保存成功');
	 	}
		//查询用户的自动回复内容
		$uid = $_SESSION['uid'];
		$userInfo = M('wxapi')->where("wxuid='{$uid}'")->find();
		$this->assign('userInfo',$userInfo);
		$this->display();
	}
	
	public function Atricle(){
		$this->show('功能未添加');
	}
	/**
	 * 为创建的方法
	 */
	public function no(){
		$this->show('功能未添加');
	}
}
